#include "base_win.hpp"
#include "defect_detect_win.hpp"
#include "hikvision_ir_camera.hpp"
#include "rknn_model_runner.hpp"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <chrono>  // 添加时间库

int main() {

    
    // 帧率计算相关变量
    int frameCount = 0;
    auto startTime = std::chrono::high_resolution_clock::now();
    auto lastFpsTime = startTime;
    HikvisionIRCamera hikvision_ir_camera;
    hikvision_ir_camera.start_check_login_status_thread();
    hikvision_ir_camera.start_frame_processor_thread();
    std::vector<uint8_t> frame(CAMERA_WIDTH * CAMERA_HEIGHT);
    while(true) {
        hikvision_ir_camera.get_grey_8bit_image(frame);
        if(frame.size() > 0) {
            frameCount++;  // 增加帧计数
            // std::cout << "Image size: " << raw.size() << std::endl;
            
            // 计算并显示帧率，每秒更新一次
            auto currentTime = std::chrono::high_resolution_clock::now();
            auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastFpsTime).count();
            
            if (elapsed >= 1000) {  // 每秒更新一次帧率
                double fps = frameCount * 1000.0 / elapsed;
                std::cout << "当前帧率: " << fps << " FPS" << std::endl;
                
                // 重置计数器和时间
                frameCount = 0;
                lastFpsTime = currentTime;
            }
        }
    }
    hikvision_ir_camera.stop_frame_processor_thread();
    hikvision_ir_camera.stop_check_login_status_thread();   
    return 0;
}
// int main()
// {
//     HikvisionIRCamera hikvision_ir_camera;
//     hikvision_ir_camera.start_check_login_status_thread();
//     hikvision_ir_camera.start_frame_processor_thread();
//     std::vector<uint8_t> frame(CAMERA_WIDTH * CAMERA_HEIGHT);
//     int ret = 0;

//     while (true)
//     {
//         ret = hikvision_ir_camera.get_grey_8bit_image(frame);
//         if (ret == 0)
//         {
//             std::cout << "Successful: get grey image." << std::endl;
//             // cv::Mat img(CAMERA_HEIGHT, CAMERA_WIDTH, CV_8UC1, frame.data());
//             // cv::imshow("666", img);

//             // if (cv::waitKey(1) == 27)
//             //     break;
//         }
//         else
//         {
//             std::cout << "Error: Failed to get grey image." << std::endl;
//         }
//     }

//     hikvision_ir_camera.stop_frame_processor_thread();
//     hikvision_ir_camera.stop_check_login_status_thread();    
//     return 0;
// }
